package Service;

import Entity.Person;
import Mapper.PersonMapper;
import Service.PersonService;
import java.time.LocalDateTime;
import java.util.List;

public class PersonServiceImpl implements PersonService {

    private PersonMapper personMapper;

    public void setPersonMapper(PersonMapper personMapper) {
        this.personMapper = personMapper;
    }

    @Override
    public boolean save(Person person) {
        // 基本校验
        if (person.getName() == null || person.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("人员姓名不能为空");
        }
        if (person.getPhone() == null || person.getPhone().trim().isEmpty()) {
            throw new IllegalArgumentException("联系电话不能为空");
        }

        // 设置默认值
        if (person.getStatus() == null) {
            person.setStatus("active"); // 默认活跃状态
        }
        if (person.getCreate_time() == null) {
            person.setCreate_time(LocalDateTime.now());
        }

        return personMapper.insert(person) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return personMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(Person person) {
        if (person.getId() == null) {
            throw new IllegalArgumentException("更新人员必须指定ID");
        }
        return personMapper.update(person) > 0;
    }

    @Override
    public Person getById(Integer id) {
        return personMapper.selectById(id);
    }

    @Override
    public List<Person> getAll() {
        return personMapper.selectAll();
    }

    @Override
    public List<Person> getByName(String name) {
        return personMapper.selectByName(name);
    }

    @Override
    public List<Person> getByPosition(String position) {
        return personMapper.selectByPosition(position);
    }

    @Override
    public List<Person> getByStatus(String status) {
        return personMapper.selectByStatus(status);
    }

    @Override
    public List<Person> getByPhone(String phone) {
        return personMapper.selectByPhone(phone);
    }

    @Override
    public List<Person> getByEmail(String email) {
        return personMapper.selectByEmail(email);
    }
}